package mule.view;

//<editor-fold defaultstate="collapsed" desc="Imports">
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.Scene;
import javafx.scene.SceneBuilder;
import javafx.scene.control.Button;
import javafx.scene.control.ButtonBuilder;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.ChoiceBoxBuilder;
import javafx.scene.control.ColorPicker;
import javafx.scene.control.ColorPickerBuilder;
import javafx.scene.control.LabelBuilder;
import javafx.scene.control.TextField;
import javafx.scene.control.TextFieldBuilder;
import javafx.scene.layout.BorderPaneBuilder;
import javafx.scene.layout.GridPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.HBoxBuilder;
import javafx.scene.layout.RegionBuilder;
import javafx.scene.layout.StackPaneBuilder;
import javafx.scene.layout.VBox;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.scene.text.TextBuilder;
import mule.model.GameModel;
import mule.model.enums.Race;
//</editor-fold>

/**
 *
 * @author stephen
 */
public class PlayerOptionView extends View
{
    Scene playerOptionScreen;
    GameModel model = GameModel.getInstance();
    private ChoiceBox colorPicker;
    private TextField nameTextField;
    private ChoiceBox raceChoice;
    private Button continueButton;
    private int playerNumber;
    
    public PlayerOptionView( int playerNumber )
    {
        this.playerNumber = playerNumber;
    }
    
    public Scene getScreen()
    {
        return createPlayerOptionScreen();
    }

    private Scene createPlayerOptionScreen()
    {
        playerOptionScreen = SceneBuilder.create()
            .width( SCREEN_WIDTH )
            .height( SCREEN_HEIGHT )
            .root(
                StackPaneBuilder.create()
                    .children(
                        RegionBuilder.create()
                        .style( "-fx-background-image: url('mule/view/images/MULE_start_bg2.png');" )
                        .build(),
                        BorderPaneBuilder.create()
                        .top(
                            playerTitle()
                        )
                        .center( 
                            optionGrid() 
                        )
                        .bottom( 
                            continueButton() 
                        )
                        .build()
                    )
                .build()
            )
        .build();

        return playerOptionScreen;
    }

    private Node playerTitle()
    {
        Text playerTitle = TextBuilder.create()
            .text("Player " + playerNumber + " Options")
            .font(Font.font( null, FontWeight.SEMI_BOLD, 50 ) )
        .build();
        HBox playerTitleBox = HBoxBuilder.create()
            .alignment( Pos.CENTER )
            .children(
                playerTitle
            )
        .build();

        return playerTitleBox;
    }
    
    private Node optionGrid()
    {
        nameTextField = TextFieldBuilder.create()
            .promptText( "Enter your name here" )
        .build();

        HBox playerNameBox = HBoxBuilder.create()
            .alignment( Pos.CENTER_LEFT )
            .spacing( 20 )
            .children(
                LabelBuilder.create()
                    .text( "Player Name" )
                .build(),
                nameTextField
            )
        .build();


        raceChoice = ChoiceBoxBuilder.create()
            .items( model.races )
            .build();
        raceChoice.getSelectionModel().selectFirst();

        HBox playerRaceBox = HBoxBuilder.create()
            .alignment( Pos.CENTER_LEFT )
            .spacing( 20 )
            .children(
                LabelBuilder.create()
                    .text( "Player Race" )
                .build(),
                raceChoice
            )
        .build();


        colorPicker = ChoiceBoxBuilder.<String>create()
            .items( FXCollections.observableArrayList( "Red", "White", "Blue", "Green" ) )
        .build();
        colorPicker.getSelectionModel().selectFirst();

        HBox playerColorPicker = HBoxBuilder.create()
            .alignment( Pos.CENTER_LEFT )
            .spacing( 20 )
            .children(
                LabelBuilder.create()
                    .text( "Player Color" )
                .build(),
                colorPicker
            )
        .build();

        GridPane optionGrid = new GridPane();
        optionGrid.hgapProperty().set(20);
        optionGrid.vgapProperty().set(20);
        optionGrid.alignmentProperty().set(Pos.CENTER);
        optionGrid.prefHeightProperty().set( 720 / 2 );
        optionGrid.prefWidthProperty().set( 1280 / 2 );
        optionGrid.add( playerNameBox,      0, 0);
        optionGrid.add( playerRaceBox,      0, 1);
        optionGrid.add( playerColorPicker,  0, 2);

        return optionGrid;
    }

    private Node continueButton()
    {
        continueButton = ButtonBuilder.create()
            .text( "Continue to Game" )
        .build();

        VBox continueBox = VBoxBuilder.create()
            .alignment( Pos.CENTER )
            .children(
                continueButton
            )
        .build();

        return continueBox;
    }

    public void addContinueListener( EventHandler event ) {
        continueButton.onActionProperty().set( event );
    }

    public String getPlayerName()
    {
        return nameTextField.getText();
    }

    public Color getPlayerColor()
    {
        if( colorPicker.getValue().equals( "Red" ) )
        {
            return Color.RED;
        }
        else if( colorPicker.getValue().equals( "White" ) )
        {
            return Color.WHITE;
        }
        else if( colorPicker.getValue().equals( "Blue" ) )
        {
            return Color.BLUE;
        }
        else
            return Color.GREEN;
    }

    public Race getPlayerRace()
    {
        return (Race)raceChoice.getValue();
    }
}